home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / casm / au116-as.exe / HEADER.CPP < prev    next >
C/C++ Source or Header  |  1994-12-13  |  8KB  |  299 lines

  1. // HEADER.CPP                                 1          1    6666
  2. // Dave Harris                                11         11   6
  3. // Compiled using Borland C++ ver 3.1       1 1        1 1   6666
  4. // 03-03-94                                  1     ..   1   6   6
  5. //                                           11111 .. 11111  666
  6. // Header and Strip options
  7. ////////////////////////////////////////////////////////////////////////
  8.  
  9. #include "au.hpp"
  10.  
  11. /*********************************************************************/
  12. /* Define Statements */
  13. /*********************/
  14.  
  15. #define PROGRAM_HEADER "HEADER" // Name of modules
  16. #define PROGRAM_STRIP  "STRIP"
  17.  
  18. /*********************************************************************/
  19.  
  20. typedef struct
  21. {
  22.     char number_headers;  // number of header files
  23.     LISTPTR header_list;
  24.  
  25.     char e_file[80];      // Empty file
  26.     long change;
  27.     long total_bytes;      // Total Bytes processed
  28.     int  header_num;      // Header number to use
  29. } HEADER_INFO;
  30.  
  31. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  32. static void print_info(AU *au)
  33. {
  34.     HEADER_INFO *in = (HEADER_INFO *)au->info;
  35.  
  36.     if (!au->no_extra)
  37.     {
  38.         au_printf_c(au, 15, "\nFiles Processed = %d\n", au->number_processed);
  39.         au_printf_c(au, 15, "Total Bytes Processed = %ld\n", in->total_bytes);
  40.  
  41.         if (in->change <= 0)
  42.         {
  43.             au_printf_c(au, 15, "Total Bytes   Saved   = %ld\n", -in->change);
  44.             au_printf_c(au, 15, "Total Percent Saved   = %s\n",
  45.                ltrim(percent_string(in->total_bytes, in->total_bytes+in->change)));
  46.         }
  47.         else
  48.         {
  49.             au_printf_c(au, 15, "Total Bytes   Lost   = %ld\n", in->change);
  50.             au_printf_c(au, 15, "Total Percent Lost   = %s\n",
  51.                ltrim(percent_string(in->total_bytes+in->change, in->total_bytes)));
  52.         }
  53.     }
  54. }
  55. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  56. static void end_program(void)
  57. {
  58.     HEADER_INFO *in = (HEADER_INFO *)glob_au->info;
  59.  
  60.     if (in->e_file[0] != '\0')
  61.         unlink(in->e_file);
  62.     return;
  63. }
  64. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  65. static void header_one(AU *au, char *file_name, PACKAGE *package)
  66. {
  67.     LIST *element;
  68.     char picked;
  69.     char string[FLENGTH];
  70.     char string2[FLENGTH];        /* build the dos commands in the string */
  71.     struct ffblk ffblk;
  72.     long   fsize_before;
  73.     int  did_rename;
  74.     HEADER_INFO *in = (HEADER_INFO *)au->info;
  75.  
  76.     if (package->header[0] == '\0')
  77.         return;
  78.  
  79.     au_printf(au, "@?6Modifying Header of @?1%s@?H\n", file_name);
  80.  
  81.     did_rename = rename_strict(au, package, au->source_directory, file_name);
  82.     au->number_processed++;
  83.  
  84.     findfirst(file_name, &ffblk, 0);
  85.     fsize_before = ffblk.ff_fsize;
  86.     in->total_bytes += fsize_before;
  87.  
  88.     if (in->header_num != 0)
  89.         picked = in->header_num;
  90.     else
  91.         picked = rand() % in->number_headers;
  92.     element = in->header_list.head;
  93.     for (int i=0; i<picked; i++)     /* skip to the right file */
  94.         element = element->next;
  95.  
  96.     substitute_macros(string, package->header, NULL, NULL, NULL, file_name);
  97.  
  98.     if (package->header_method == HEADER_AT)
  99.         sprintf(string2, "ECHO @%s | %s", element->data, string);
  100.     else if (package->header_method == HEADER_RED)
  101.         sprintf(string2, "%s <%s", string, element->data);
  102.     else
  103.         sprintf(string2, "%s %s", string, element->data);
  104.  
  105.     if (!au->simulate)
  106.     {
  107.         execute_raw(au, string2);
  108.         if (did_rename)
  109.             rename_strict_back(au->source_directory, file_name);
  110.         fix_flist(au, file_name, file_name);
  111.  
  112.         findfirst(file_name, &ffblk, 0);
  113.         in->change += ffblk.ff_fsize - fsize_before;
  114.     }
  115.  
  116. }
  117. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  118. static int header(AU *au, char *file_name)
  119. {
  120.     HANDLE handle;
  121.     ARC_HANDLE arc_handle;
  122.     struct ftime ftime_hold;
  123.  
  124.     check_for_key();
  125.  
  126.     arc_handle.init(au, file_name);
  127.     arc_handle.get_time(&ftime_hold);
  128.     arc_handle.deinit(au);
  129.     if (arc_handle.type > 0)
  130.     {
  131.         header_one(au, file_name, &au->package[arc_handle.type]);
  132.  
  133.         if (!au->simulate)
  134.         {
  135.             handle.open(au, file_name);
  136.             handle.set_time(&ftime_hold);
  137.             handle.close();
  138.         }
  139.     }
  140.     return 0;
  141. }
  142. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  143. static void add_header(AU *au, char *string)
  144. {
  145.     struct ffblk ffblk;
  146.     HEADER_INFO *in = (HEADER_INFO *)au->info;
  147.  
  148.     if (findfirst(string,&ffblk,0))
  149.     {
  150.         au_printf_error(au, "Header_file '%s' Not found", string);
  151.         exit(1);
  152.     }
  153.     in->header_list.add(string);
  154.     in->number_headers++;
  155. }
  156. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  157. static void ReadCFGInfo(AU *au, CFG_HANDLE *cfg_handle)
  158. {
  159.     char string[200],
  160.          string2[200],
  161.          string3[200];
  162.  
  163.     for(EVER)
  164.     {
  165.         if (cfg_handle->read_line(au, string)==EOF)
  166.             break;
  167.  
  168.         split_string(string, string2);
  169.         split_string(string, string3);
  170.  
  171.         if (string2[0] == '\0')
  172.             continue;
  173.  
  174.         strcpy(au->curOpt, string2);
  175.         au->curVal = string3;
  176.         switch (toupper(string2[1]) << 8 | toupper(string2[0]))
  177.         {
  178.             case 'BE':                                          // Begin
  179.                 return;
  180.             case 'HE':
  181.                 add_header(au, string3);
  182.                 break;
  183.             default:
  184.                 cfg_handle->invalid_option(au, string2);
  185.         }
  186.     }
  187. }
  188. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  189. static BYTE parse_comm_line(AU *au, char option, char *cur_argv,
  190.                             PARSE_TYPE type)
  191. {
  192.     HEADER_INFO *in = (HEADER_INFO *)au->info;
  193.  
  194.     switch (type)
  195.     {
  196.     case PARSE_PARAM_OPTION:
  197.         switch (option)
  198.         {
  199.         case 'A':
  200.             add_header(au, cur_argv);
  201.             break;
  202.         case 'H':
  203.             /* add clean up code right here*/
  204.             in->header_list.head = in->header_list.tail = NULL;
  205.             in->number_headers = 0;
  206.             add_header(au, cur_argv);
  207.             break;
  208.         case 'N':
  209.             in->header_num = atoi(cur_argv);
  210.             break;
  211.         case '?':
  212.             au_standard_opt_header(au, "Header",
  213.                "@?3-N@?H#              Used header file #\n"
  214.                "@?3-A@?H<File>         Add header file\n"
  215.                "@?3-H@?H<File>         use this Header file only\n");
  216.             exit (0);
  217.         default:
  218.             au_invalid_option(au, PROGRAM_HEADER, option);
  219.         }
  220.         return TRUE;
  221.     case PARSE_POST_CHECK:
  222.         if (in->header_num > in->number_headers)
  223.         {
  224.             au_printf_error(au, "\nOnly %d headers", in->number_headers);
  225.             exit(1);
  226.         }
  227.         if (in->number_headers==0)
  228.         {
  229.             au_printf_error(au, "No header_file specified");
  230.             exit(1);
  231.         }
  232.         return TRUE;
  233.     }
  234.     return FALSE;
  235. }
  236. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  237. static BYTE parse_comm_line2(AU *au, char option, char *, PARSE_TYPE type)
  238. {
  239.     switch (type)
  240.     {
  241.     case PARSE_PARAM_OPTION:
  242.         switch (option)
  243.         {
  244.         case '?':
  245.             au_standard_opt_header(au, "STrip", NULL);
  246.             exit (0);
  247.         default:
  248.             au_invalid_option(au, PROGRAM_STRIP, option);
  249.         }
  250.         return TRUE;
  251.     }
  252.     return FALSE;
  253. }
  254. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  255. int main_header(AU *au, int argc, char *argv[])
  256. {
  257.     HEADER_INFO *in;
  258.  
  259.     in = new HEADER_INFO;
  260.     memset(in, '\0', sizeof(HEADER_INFO));
  261.     au->info = in;
  262.  
  263.     srand(time(NULL) % 37);
  264.  
  265.     ReadGlobalCFGInfo(au, au->cfg_file, PROGRAM_HEADER, ReadCFGInfo);
  266.     generic_parse_comm_line(au, argc, argv, parse_comm_line);
  267.  
  268.     process_files(au, header);
  269.     print_info(au);
  270.  
  271.     return 0;
  272. }
  273. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  274. int main_strip(AU *au, int argc, char *argv[])
  275. {
  276.     HEADER_INFO *in;
  277.  
  278.     in = new HEADER_INFO;
  279.     memset(in, '\0', sizeof(HEADER_INFO));
  280.     au->info = in;
  281.  
  282.     ReadGlobalCFGInfo(au, au->cfg_file, PROGRAM_STRIP, ReadCFGInfo);
  283.     generic_parse_comm_line(au, argc, argv, parse_comm_line2);
  284.  
  285.     atexit(end_program);
  286.  
  287.     build_fname(in->e_file, au->cur_directory, "e__file.$$$");
  288.     close(creat(in->e_file, S_IWRITE));   /* Create an Empty file */
  289.  
  290.     add_header(au, in->e_file);
  291.     in->number_headers = 1;
  292.  
  293.     process_files(au, header);
  294.     print_info(au);
  295.  
  296.     return 0;
  297. }
  298.  
  299.